package com.jiukuaidao.zhulin.module.order;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.jiukuaidao.library.base.BaseActivity;
import com.jiukuaidao.library.common.dialog.CommDialog;
import com.jiukuaidao.library.common.dialog.ICallBack;
import com.jiukuaidao.library.util.ToastUtil;
import com.jiukuaidao.library.widget.smartlayout.SmartTabLayout;
import com.jiukuaidao.zhulin.R;
import com.jiukuaidao.zhulin.data.TaskProvider;
import com.jiukuaidao.zhulin.module.CommContract;
import com.jiukuaidao.zhulin.module.CommPagerAdapter;
import com.jiukuaidao.zhulin.module.CommPresenter;
import com.jiukuaidao.zhulin.module.pay.wechat.WXPayContract;
import com.jiukuaidao.zhulin.module.pay.wechat.WXPayPresenter;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * Author : PaiTaSuo
 * Date : 2016/6/2
 * Description :
 */
public class OrderTabActivity extends BaseActivity implements View.OnClickListener, CommContract.orderView, WXPayContract.View {
    @BindView(R.id.titile_text)
    TextView mTitleText;
    @BindView(R.id.titile_left_imageview)
    ImageView mLeftImg;
    @BindView(R.id.tabLayout)
    SmartTabLayout mTabLayout;
    @BindView(R.id.viewPager)
    ViewPager mViewPager;

    private List<Fragment> mFragments = new ArrayList<Fragment>();
    List<String> mTypelist = new ArrayList<String>();
    private int mTypeTab;
    private Bundle bundle;
    private CommPresenter mCommPresenter;
    private String mOrderNo;
    private WXPayPresenter mWXPayPresenter;

    @Override
    protected View getTargetView() {
        return null;
    }

    @Override
    protected int getContentLayoutId() {
        return R.layout.activity_order_tab;
    }

    @Override
    protected void handleBundle(Bundle bundle) {
        mTypeTab = bundle.getInt("type");

    }

    @Override
    protected void init() {
        EventBus.getDefault().register(OrderTabActivity.this);
        mTitleText.setText("我的订单");
        setOnClickListenser();
        //订单状态 0：全部 1:待支付 2:待收货 3:待评价4:退款
        String[] typeName = new String[]{"全部订单", "待支付", "待收货", "待评价"};
        for (int i = 0; i < typeName.length; i++) {
            mTypelist.add(typeName[i]);
        }
        for (int i = 0; i < mTypelist.size(); i++) {
            bundle = new Bundle();
            bundle.putString("lableName", mTypelist.get(i));
            bundle.putString("status", String.valueOf(i));
            mFragments.add(OrderListFragment.newInstance(OrderListFragment.class, bundle));
        }
        CommPagerAdapter adapter = new CommPagerAdapter(mFragments, getSupportFragmentManager(), mTypelist);
        mViewPager.setAdapter(adapter);
        mTabLayout.setViewPager(mViewPager);
        mViewPager.setCurrentItem(mTypeTab);
        mCommPresenter = new CommPresenter(TaskProvider.provideTasksRepository(this), this);
        mWXPayPresenter = new WXPayPresenter(TaskProvider.provideTasksRepository(this), this);

    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.titile_left_imageview:
                finish();
                break;
        }

    }

    public void setOnClickListenser() {
        mLeftImg.setOnClickListener(this);
    }

    @Subscribe
    public void onEventClickItem(EventClickOrder clickOrder) {
        mOrderNo = clickOrder.mOrderList.orderNo;
        if (!clickOrder.isButton) {
            Bundle bundle = new Bundle();
            bundle.putString("orderNumber", clickOrder.mOrderList.orderNo);
            intentJump(OrderDetailActivity.class, bundle);
        } else {
            switch (clickOrder.mOrderList.status) {
                case OrderState.ORDER_UNPAID: //去支付
                    if (clickOrder.mOrderList.payWay == 3) { //微信支付
                        mWXPayPresenter.loadData();
                    }
                    break;
                case OrderState.ORDER_SHIP: //签收
                    showDialogTwoBtn(getString(R.string.confirm_receipt), getString(R.string.receipt), getString(R.string.cancel), new ICallBack.TwoCallBack() {
                        @Override
                        public void left(CommDialog dialog) {
                            //调用订单签收接口
                            mCommPresenter.orderSign();
                        }

                        @Override
                        public void right(CommDialog dialog) {
                        }
                    });
                    break;
                case OrderState.ORDER_RECEIVED://评价:
                    Bundle bundle = new Bundle();
                    bundle.putString("orderNumber",clickOrder.mOrderList.orderNo);
                    intentJump(OrdersCommentsActivity.class,bundle);

                    break;
            }
        }
    }

    @Override
    public String getOrderNumber() {
        return mOrderNo;
    }

    @Override
    public void refreshInfo() {
        //刷新列表
    }

    @Override
    public void toast(String msg) {

    }

}
